Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | import { apiService } from './api';
import { ApiResult } from '@/types/api';
interface LoginHistoryFilters {
user_id?: number;
username?: string;
start_date?: string;
end_date?: string;
ip_address?: string;
page?: number;
limit?: number;
}
interface ViewingHistoryFilters {
user_id?: number;
username?: string;
content_type?: string;
start_date?: string;
end_date?: string;
ip_address?: string;
page?: number;
limit?: number;
}
interface LoginHistoryEntry {
id: number;
user_id: number;
username: string;
login_timestamp: string;
ip_address?: string;
device_info?: string;
user_agent?: string;
success: boolean;
}
interface ViewingHistoryEntry {
id: number;
user_id: number;
username: string;
content_type: string;
content_id?: number;
content_title: string;
episode_title?: string;
season_number?: number;
episode_number?: number;
start_timestamp: string;
ip_address?: string;
device_info?: string;
user_agent?: string;
}
interface LoginHistoryResponse {
logs: LoginHistoryEntry[];
total: number;
page: number;
limit: number;
total_pages: number;
}
interface ViewingHistoryResponse {
logs: ViewingHistoryEntry[];
total: number;
page: number;
limit: number;
total_pages: number;
}
interface DashboardMetrics {
total_logins_today: number;
unique_active_users: number;
failed_login_attempts: number;
total_content_views: number;
most_viewed_content: PopularContent[];
device_distribution: DeviceStats[];
hourly_activity: HourlyActivity[];
users_multiple_devices: MultiDeviceUser[];
}
interface PopularContent {
content_id?: number;
content_title: string;
content_type: string;
view_count: number;
}
interface DeviceStats {
device_type: string;
count: number;
percentage: number;
}
interface HourlyActivity {
hour: number;
login_count: number;
view_count: number;
}
interface MultiDeviceUser {
user_id: number;
username: string;
device_count: number;
devices: string[];
}
export class UserActivityService {
// Get login history (admin only)
async getLoginHistory(filters: LoginHistoryFilters): Promise<ApiResult<LoginHistoryResponse>> {
try {
const params = new URLSearchParams();
if (filters.user_id) params.append('user_id', filters.user_id.toString());
if (filters.username) params.append('username', filters.username);
if (filters.start_date) params.append('start_date', filters.start_date);
if (filters.end_date) params.append('end_date', filters.end_date);
if (filters.ip_address) params.append('ip_address', filters.ip_address);
if (filters.page) params.append('page', filters.page.toString());
if (filters.limit) params.append('limit', filters.limit.toString());
const queryString = params.toString();
const url = `/api/admin/activity/login-history${queryString ? `?${queryString}` : ''}`;
const result = await apiService.get<LoginHistoryResponse>(url);
return result;
} catch (error) {
console.error('Error fetching login history:', error);
return {
success: false,
error: {
error: 'Failed to fetch login history',
details: error instanceof Error ? error.message : 'Failed to fetch login history',
timestamp: new Date().toISOString()
}
};
}
}
// Get viewing history (admin only)
async getViewingHistory(filters: ViewingHistoryFilters): Promise<ApiResult<ViewingHistoryResponse>> {
try {
const params = new URLSearchParams();
if (filters.user_id) params.append('user_id', filters.user_id.toString());
if (filters.username) params.append('username', filters.username);
if (filters.content_type) params.append('content_type', filters.content_type);
if (filters.start_date) params.append('start_date', filters.start_date);
if (filters.end_date) params.append('end_date', filters.end_date);
if (filters.ip_address) params.append('ip_address', filters.ip_address);
if (filters.page) params.append('page', filters.page.toString());
if (filters.limit) params.append('limit', filters.limit.toString());
const queryString = params.toString();
const url = `/api/admin/activity/viewing-history${queryString ? `?${queryString}` : ''}`;
const result = await apiService.get<ViewingHistoryResponse>(url);
return result;
} catch (error) {
console.error('Error fetching viewing history:', error);
return {
success: false,
error: {
error: 'Failed to fetch viewing history',
details: error instanceof Error ? error.message : 'Failed to fetch viewing history',
timestamp: new Date().toISOString()
}
};
}
}
// Get dashboard metrics (admin only)
async getDashboardMetrics(): Promise<ApiResult<DashboardMetrics>> {
try {
const result = await apiService.get<DashboardMetrics>('/api/admin/activity/dashboard-metrics');
return result;
} catch (error) {
console.error('Error fetching dashboard metrics:', error);
return {
success: false,
error: {
error: 'Failed to fetch dashboard metrics',
details: error instanceof Error ? error.message : 'Failed to fetch dashboard metrics',
timestamp: new Date().toISOString()
}
};
}
}
}
export const userActivityService = new UserActivityService();
|